package blackberryApp;

import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.Hashtable;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import blackberryAppCom.BBApp_Common;
import blackberryAppBus.BBApp_Business;

/**
 * Servlet implementation class BBApp_cMainScreen
 */
public class BBApp_cMainScreen extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public BBApp_cMainScreen() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		BBApp_Business lObjBBApp_Business = new BBApp_Business();
		
		int lIntHidMode = BBApp_Common.fnChkInteger(BBApp_Common.fnGetValue(req.getParameter("hidMode")));
		if (lIntHidMode == 0) {
			lIntHidMode = 1;
		}
		
		String strNoKartu 		= BBApp_Common.fnGetValue(req.getParameter("hidNoKartu"));
		String strTglLahir 		= BBApp_Common.fnGetValue(req.getParameter("hidTglLahir"));
		String strNama 			= BBApp_Common.fnGetValue(req.getParameter("hidNama"));
		String strNoPolis		= BBApp_Common.fnGetValue(req.getParameter("hidNoPolis"));
		String strNoReg			= BBApp_Common.fnGetValue(req.getParameter("hidNoReg"));
		String strUmur			= BBApp_Common.fnGetValue(req.getParameter("hidUmur"));
		String strPerusahaan 	= BBApp_Common.fnGetValue(req.getParameter("hidPerusahaan"));
		String strProgId 		= BBApp_Common.fnGetValue(req.getParameter("hidProgramId"));
		String strPlanId 		= BBApp_Common.fnGetValue(req.getParameter("hidPlanId"));
		String strProgName 		= BBApp_Common.fnGetValue(req.getParameter("hidProgramName"));
		String strPlanName 		= BBApp_Common.fnGetValue(req.getParameter("hidPlanName"));
		String strRecNo			= BBApp_Common.fnGetValue(req.getParameter("hidRecNo"));
		String strMaxNo			= BBApp_Common.fnGetValue(req.getParameter("hidMaxNo"));
		
		BBApp_cjbBBApp lObjBBApp_cjbBBApp = new BBApp_cjbBBApp();
		lObjBBApp_cjbBBApp.setStrNoKartu(strNoKartu);
		lObjBBApp_cjbBBApp.setStrTglLahir(strTglLahir);
		lObjBBApp_cjbBBApp.setStrNama(strNama);
		lObjBBApp_cjbBBApp.setStrNoPolis(strNoPolis);
		lObjBBApp_cjbBBApp.setStrNoReg(strNoReg);
		lObjBBApp_cjbBBApp.setStrUmur(strUmur);
		lObjBBApp_cjbBBApp.setStrPerusahaan(strPerusahaan);
		lObjBBApp_cjbBBApp.setStrProgramId(strProgId);
		lObjBBApp_cjbBBApp.setStrPlanId(strPlanId);
		lObjBBApp_cjbBBApp.setStrProgramName(strProgName);
		lObjBBApp_cjbBBApp.setStrPlanName(strPlanName);
		lObjBBApp_cjbBBApp.setStrRecNo(strRecNo);
		lObjBBApp_cjbBBApp.setStrMaxNo(strMaxNo);
		
		req.getSession().removeAttribute("lHstDataPolis");
		req.getSession().removeAttribute("PROG_NAME");
		req.getSession().removeAttribute("PLAN_NAME");
		req.getSession().removeAttribute("DATA_BENEFIT");
		
		req.getSession().removeAttribute("RESERVE_TYPE_CLAIM_VALUE");
		req.getSession().removeAttribute("lHstDataClaim");
		
		switch (lIntHidMode) {
		
			case 5: // fnViewKlaim
				try {
//					Hashtable lHstReserve = lObjBBApp_Business.fnRetrieveReservetype();
//					req.getSession().setAttribute("RESERVE_TYPE_CLAIM_VALUE", lHstReserve);
//				  	
//					Hashtable lHstDataClaim = lObjBBApp_Business.fnGetDataClaimClaiment(strNoPolis, strNoReg, "", "");
//				  	req.getSession().setAttribute("lHstDataClaim", lHstDataClaim);
					
					BBApp_cjbBBApp lObjBBApp_cjbBBAppResult = new BBApp_cjbBBApp();
					lObjBBApp_cjbBBAppResult = lObjBBApp_Business.fnRetrieveDetailPeserta(lObjBBApp_cjbBBApp);
					
					strNoPolis = lObjBBApp_cjbBBAppResult.getStrNoPolis();
					strNoReg = lObjBBApp_cjbBBAppResult.getStrNoReg();
					
					Hashtable lHstDataClaim = lObjBBApp_Business.fnGetDataClaim(strNoPolis, strNoReg, strRecNo);
					if (lHstDataClaim.size() > 0) {
						req.getSession().setAttribute("lHstDataClaim", lHstDataClaim);
//						strMaxNo = (String) lHstDataClaim.get("MaxNo");
//						lObjBBApp_cjbBBAppResult.setStrMaxNo(strMaxNo);
//						strRecNo = (String) lHstDataClaim.get("RecNo");
//						lObjBBApp_cjbBBAppResult.setStrRecNo(strRecNo);
					}
					
					XMLOutput(req, resp, lObjBBApp_cjbBBAppResult);
					
//					req.setAttribute("lObjBBApp_cjbBBApp",lObjBBApp_cjbBBApp);
//					fnForwardToJsp(req, resp, "/BBApp_MainScreen.jsp");
					return;
				}
				catch(Exception e) {
					BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(case 5) : ","Exception : "+e);
					req.setAttribute("ErrorCode","2000");
					//getServletConfig().getServletContext().getRequestDispatcher("UTIL_cERRORPAGE").forward(req,resp);	
					return;
				}
			
 				
			case 4: // coba xml
				try {
//					Hashtable lHstDataClaim = lObjBBApp_Business.fnGetDataClaim(strNoPolis, strNoReg, strRecNo);
//					if (lHstDataClaim.size() > 0) {
//						req.getSession().setAttribute("lHstDataClaim", lHstDataClaim);
//						strMaxNo = (String) lHstDataClaim.get("MaxNo");
//						lObjBBApp_cjbBBApp.setStrMaxNo(strMaxNo);
//						strRecNo = (String) lHstDataClaim.get("RecNo");
//						lObjBBApp_cjbBBApp.setStrRecNo(strRecNo);
//					}
					
					DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
			        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
			        Document doc = docBuilder.newDocument();
			        String strXMLString = createXmlTree(doc, req, resp, lObjBBApp_cjbBBApp);
			        
			        ServletOutputStream out = resp.getOutputStream();
			        resp.setContentType("text/xml");
			        InputStream in = new ByteArrayInputStream(strXMLString.getBytes());
			        byte[] buffer = strXMLString.getBytes();
			        int intSize = in.read(buffer);
				    if (intSize != -1) {
				      out.write(buffer, 0, intSize);
				    }
				    in.close();
				    out.flush();
			        
			     // bikin file dan tulis isi nya 
//			        File file = new File("c:/newxml.xml");
//			        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
//			        bw.write(xmlString);
//			        bw.flush();
//			        bw.close();
					
					return;
				}
				catch(Exception e) {
					BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(case 4) : ","Exception : "+e);
					req.setAttribute("ErrorCode","2000");
					//getServletConfig().getServletContext().getRequestDispatcher("UTIL_cERRORPAGE").forward(req,resp);	
					return;
				}
			
			case 3: // fnShowDetail()
				try {
//					Hashtable lHstDataPolis = new Hashtable();
//				    if (req.getSession().getAttribute("lHstDataPolis") != null) {
//				    	lHstDataPolis = (Hashtable) req.getSession().getAttribute("lHstDataPolis");
//				    } else {
//				    	lHstDataPolis = lObjBBApp_Business.fnShowDataPolis(strNoPolis, strNoReg);
//				    }
//				    req.getSession().setAttribute("lHstDataPolis", lHstDataPolis);
//				    
//				    String lStrProgId = BBApp_Common.fnGetValue(req.getParameter("hidProgramId"));
//					String lStrPlanId = BBApp_Common.fnGetValue(req.getParameter("hidPlanId"));
//					String lStrProgName = BBApp_Common.fnGetValue(req.getParameter("hidProgramName"));
//					String lStrPlanName = BBApp_Common.fnGetValue(req.getParameter("hidPlanName"));
//					Hashtable lHstData = lObjBBApp_Business.fnGetBenefitLim(lStrProgId, lStrPlanId);
//					req.getSession().setAttribute("PROG_NAME", lStrProgName);
//					req.getSession().setAttribute("PLAN_NAME", lStrPlanName);
//					req.getSession().setAttribute("DATA_BENEFIT", lHstData);
//					
//					lObjBBApp_cjbBBApp.setStrProgramId(lStrProgId);
//					lObjBBApp_cjbBBApp.setStrPlanId(lStrPlanId);
//					lObjBBApp_cjbBBApp.setStrProgramName(lStrProgName);
//					lObjBBApp_cjbBBApp.setStrPlanName(lStrPlanName);
					
//					BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(XMLOutput) : ","Case 3");
					
					BBApp_cjbBBApp lObjBBApp_cjbBBAppResult = new BBApp_cjbBBApp();
					lObjBBApp_cjbBBAppResult = lObjBBApp_Business.fnRetrieveDetailPeserta(lObjBBApp_cjbBBApp);
					
//					BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(XMLOutput) : ","Case 3, Cek 1");

          strNoPolis = lObjBBApp_cjbBBAppResult.getStrNoPolis();
					strNoReg = lObjBBApp_cjbBBAppResult.getStrNoReg();
					
					Hashtable lHstDataPolis = lObjBBApp_Business.fnShowDataPolis(strNoPolis, strNoReg);
					req.getSession().setAttribute("lHstDataPolis", lHstDataPolis);
					
//					BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(XMLOutput) : ","Case 3, Cek 2");
				    
				    Hashtable htNewPlan = (Hashtable) lHstDataPolis.get("NEW_PLAN");
				    int intNewPlan = htNewPlan.size();
				    if (intNewPlan > 0) {
				    	
//				    	BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(XMLOutput) : ","Case 3, Cek 3");
				    	
					    for (int a=1; a<=intNewPlan; a++) {
					    	
//					    	BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(XMLOutput) : ","Case 3, Cek 4." + a);
					    	
					    	String[] arrNewPlan = (String[]) htNewPlan.get(String.valueOf(a));
//					    	st[0] = BBApp_Common.fnGetValue(lRsFetch.getString("PROGRAM_NAME"));
//							st[1] = BBApp_Common.fnGetValue(lRsFetch.getString("PLAN_NAME"));
//							st[2] = BBApp_Common.fnGetValue(lRsFetch.getString("TSI"));
//							st[3] = BBApp_Common.fnGetValue(lRsFetch.getString("NET_PREMIUM"));
//							st[4] = BBApp_Common.fnGetValue(lRsFetch.getString("PROGRAM_ID"));
//							st[5] = BBApp_Common.fnGetValue(lRsFetch.getString("PLAN_ID"));
//							st[6] = lStrSisa;
//					    	st[7] = BBApp_Common.fnGetValue(lRsFetch.getString("GROUP_PROG_NAME2"));
					    	String lStrPlanName = arrNewPlan[1];
					    	if (lStrPlanName.equalsIgnoreCase(strPlanName)) {
					    		String lStrProgId = arrNewPlan[4];
						    	String lStrPlanId = arrNewPlan[5];
						    	Hashtable lHstData = lObjBBApp_Business.fnGetBenefitLim(lStrProgId, lStrPlanId);
							    req.getSession().setAttribute("DATA_BENEFIT", lHstData);
							    req.getSession().setAttribute("PLAN_NAME", strPlanName);
					    	}
					    }
				    }
				    
				    XMLOutput(req, resp, lObjBBApp_cjbBBAppResult);
					
//					req.setAttribute("lObjBBApp_cjbBBApp",lObjBBApp_cjbBBApp);
//					fnForwardToJsp(req, resp, "/BBApp_MainScreen.jsp");
					return;
				}
				catch(Exception e) {
					BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(case 3) : ","Exception : "+e);
					req.setAttribute("ErrorCode","2000");
					//getServletConfig().getServletContext().getRequestDispatcher("UTIL_cERRORPAGE").forward(req,resp);	
					return;
				}
			
			case 2: // fnViewPolis
				try {
					BBApp_cjbBBApp lObjBBApp_cjbBBAppResult = new BBApp_cjbBBApp();
					lObjBBApp_cjbBBAppResult = lObjBBApp_Business.fnRetrieveDetailPeserta(lObjBBApp_cjbBBApp);
					
					strNoPolis = lObjBBApp_cjbBBAppResult.getStrNoPolis();
					strNoReg = lObjBBApp_cjbBBAppResult.getStrNoReg();
					
					Hashtable lHstDataPolis = lObjBBApp_Business.fnShowDataPolis(strNoPolis, strNoReg);
				    req.getSession().setAttribute("lHstDataPolis", lHstDataPolis);
					
				    XMLOutput(req, resp, lObjBBApp_cjbBBAppResult);
				    
//					req.setAttribute("lObjBBApp_cjbBBApp",lObjBBApp_cjbBBApp);
//					fnForwardToJsp(req, resp, "/BBApp_MainScreen.jsp");
					return;
				}
				catch(Exception e) {
					BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(case 2) : ","Exception : "+e);
					req.setAttribute("ErrorCode","2000");
					//getServletConfig().getServletContext().getRequestDispatcher("/UTIL_cERRORPAGE").forward(req,resp);	
					return;
				}
			
			case 1: // dari blackberry, login SIMAS SEHAT
				try {
//					BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(case 1) : ","Case 1 - 1");
					BBApp_cjbBBApp lObjBBApp_cjbBBAppResult = new BBApp_cjbBBApp();
					lObjBBApp_cjbBBAppResult = lObjBBApp_Business.fnRetrieveDetailPeserta(lObjBBApp_cjbBBApp);
					
					XMLOutput(req, resp, lObjBBApp_cjbBBAppResult);
				    
//					req.setAttribute("lObjBBApp_cjbBBApp",lObjBBApp_cjbBBAppResult);
//					fnForwardToJsp(req, resp, "/BBApp_MainScreen.jsp");
					return;
				}
				catch(Exception e) {
					BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(case 1) : ","Exception : "+e);
					req.setAttribute("ErrorCode","2000");
					//getServletConfig().getServletContext().getRequestDispatcher("/UTIL_cERRORPAGE").forward(req,resp);	
					return;
				}
				
			case 0: // Redirect to error page
				BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen : ","Undefined hidMode or hidMode is null : "+lIntHidMode);
				req.setAttribute("ErrorCode","2001");
				//getServletConfig().getServletContext().getRequestDispatcher("/UTIL_cERRORPAGE").forward(req,resp);
				return;
		}
	}
	
	/**
	 * XMLOutput
	 */
	public void XMLOutput(HttpServletRequest req, HttpServletResponse resp, BBApp_cjbBBApp lObjBBApp_cjbBBAppResult) {
		try {
			DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
	        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
	        Document doc = docBuilder.newDocument();
	        
	        String strXMLString = createXmlTree(doc, req, resp, lObjBBApp_cjbBBAppResult);
	        
	        ServletOutputStream out = resp.getOutputStream();
	        resp.setContentType("text/xml");
	        InputStream in = new ByteArrayInputStream(strXMLString.getBytes());
	        byte[] buffer = strXMLString.getBytes();
	        int intSize = in.read(buffer);
		    if (intSize != -1) {
		      out.write(buffer, 0, intSize);
		    }
		    
		    in.close();
		    out.flush();
		}
		catch (Exception exp) {
			BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(XMLOutput) : ","Exception : "+exp);
		}
	}
	
	
	/**
	 * createXmlTree
	 */
	public String createXmlTree(Document doc, HttpServletRequest req, HttpServletResponse resp, BBApp_cjbBBApp lObjBBApp_cjbBBAppResult) throws Exception {
        //This method creates an element node
		
		String strNoPolis = BBApp_Common.fnGetValue(lObjBBApp_cjbBBAppResult.getStrNoPolis());
		String strNoReg = BBApp_Common.fnGetValue(lObjBBApp_cjbBBAppResult.getStrNoReg());
		String strNama = BBApp_Common.fnGetValue(lObjBBApp_cjbBBAppResult.getStrNama());
		String strUmur = BBApp_Common.fnGetValue(lObjBBApp_cjbBBAppResult.getStrUmur());
		String strTglLahir = BBApp_Common.fnGetValue(lObjBBApp_cjbBBAppResult.getStrTglLahir());
		String strPerusahaan = BBApp_Common.fnGetValue(lObjBBApp_cjbBBAppResult.getStrPerusahaan());
		String strNoKartu = BBApp_Common.fnGetValue(lObjBBApp_cjbBBAppResult.getStrNoKartu());
		
		Element eRoot = doc.createElement("GlobalInfo");
		doc.appendChild(eRoot);
		
		Element ePeserta = doc.createElement("Peserta");
		eRoot.appendChild(ePeserta);
		
			Element eNoKartu = doc.createElement("NoKartu");
			ePeserta.appendChild(eNoKartu);
			Text txtNoKartu = doc.createTextNode(strNoKartu);
			eNoKartu.appendChild(txtNoKartu);
			
			Element eNoPolis = doc.createElement("NoPolis");
			ePeserta.appendChild(eNoPolis);
			Text txtNoPolis = doc.createTextNode(strNoPolis);
			eNoPolis.appendChild(txtNoPolis);
			
			Element eNoReg = doc.createElement("NoPeserta");
			ePeserta.appendChild(eNoReg);
			Text txtNoReg = doc.createTextNode(strNoReg);
			eNoReg.appendChild(txtNoReg);
			
			Element eNama = doc.createElement("NamaPeserta");
			ePeserta.appendChild(eNama);
			Text txtNama = doc.createTextNode(strNama);
			eNama.appendChild(txtNama);
			
			Element eTglLahir = doc.createElement("TglLahir");
			ePeserta.appendChild(eTglLahir);
			Text txtTglLahir = doc.createTextNode(strTglLahir);
			eTglLahir.appendChild(txtTglLahir);
			
			Element eUmur = doc.createElement("Umur");
			ePeserta.appendChild(eUmur);
			Text txtUmur = doc.createTextNode(strUmur);
			eUmur.appendChild(txtUmur);
			
			Element ePerusahaan = doc.createElement("NamaPerusahaan");
			ePeserta.appendChild(ePerusahaan);
			Text txtPerusahaan = doc.createTextNode(strPerusahaan);
			ePerusahaan.appendChild(txtPerusahaan);
		
		if (req.getSession().getAttribute("lHstDataPolis") != null) {
			Hashtable lHstDataPolis = (Hashtable) req.getSession().getAttribute("lHstDataPolis");
			Hashtable lHstPlanNew = (Hashtable) lHstDataPolis.get("NEW_PLAN");
			for (int a=1; a<=lHstPlanNew.size(); a++) {
//				st[0] = BBApp_Common.fnGetValue(lRsFetch.getString("PROGRAM_NAME"));
//				st[1] = BBApp_Common.fnGetValue(lRsFetch.getString("PLAN_NAME"));
//				st[2] = BBApp_Common.fnGetValue(lRsFetch.getString("TSI"));
//				st[3] = BBApp_Common.fnGetValue(lRsFetch.getString("NET_PREMIUM"));
//				st[4] = BBApp_Common.fnGetValue(lRsFetch.getString("PROGRAM_ID"));
//				st[5] = BBApp_Common.fnGetValue(lRsFetch.getString("PLAN_ID"));
//				st[6] = lStrSisa;
//				st[7] = BBApp_Common.fnGetValue(lRsFetch.getString("GROUP_PROG_NAME2"));
				String[] strDataPlan = (String[]) lHstPlanNew.get(String.valueOf(a));
				String strPlan = strDataPlan[1];
				String strPremi = BBApp_Common.fnFormatNumberInd(strDataPlan[3]);
				String strSisaLimit = BBApp_Common.fnFormatNumberInd(strDataPlan[6]);
				String strProgramId = strDataPlan[4];
				String strPlanId = strDataPlan[5];
				String strProgramName = strDataPlan[0];
				String strJnsJaminan = strDataPlan[7];
				
				Element ePlan = doc.createElement("Plan");
				eRoot.appendChild(ePlan);
				
					Element eKodePlan = doc.createElement("KodePlan");
					ePlan.appendChild(eKodePlan);
					Text txtKodePlan = doc.createTextNode(strPlan);
					eKodePlan.appendChild(txtKodePlan);
					
					Element eJnsJaminan = doc.createElement("JnsJaminan");
					ePlan.appendChild(eJnsJaminan);
					Text txtJnsJaminan = doc.createTextNode(strJnsJaminan);
					eJnsJaminan.appendChild(txtJnsJaminan);
					
					Element ePremi = doc.createElement("Premi");
					ePlan.appendChild(ePremi);
					Text txtPremi = doc.createTextNode(strPremi);
					ePremi.appendChild(txtPremi);
					
					Element eSisaLimit = doc.createElement("SisaLimit");
					ePlan.appendChild(eSisaLimit);
					Text txtSisaLimit = doc.createTextNode(strSisaLimit);
					eSisaLimit.appendChild(txtSisaLimit);
					
//					Element eProgramId = doc.createElement("ProgramId");
//					ePlan.appendChild(eProgramId);
//					Text txtProgramId = doc.createTextNode(strProgramId);
//					eProgramId.appendChild(txtProgramId);
//					
//					Element ePlanId = doc.createElement("PlanId");
//					ePlan.appendChild(ePlanId);
//					Text txtPlanId = doc.createTextNode(strPlanId);
//					ePlanId.appendChild(txtPlanId);
//					
//					Element eProgramName = doc.createElement("ProgramName");
//					ePlan.appendChild(eProgramName);
//					Text txtProgramName = doc.createTextNode(strProgramName);
//					ePlanId.appendChild(txtProgramName);
					
			}
		}
		
		if (req.getSession().getAttribute("DATA_BENEFIT") != null) {
			
			BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(XMLOutput) : ","createXmlTree, data benefit");
			
			String strPlanName = req.getSession().getAttribute("PLAN_NAME").toString();
			Hashtable htBenefit = (Hashtable) req.getSession().getAttribute("DATA_BENEFIT");
			int intBenefit = htBenefit.size();
			for (int a=1; a<=intBenefit; a++) {
				String[] arrBenefit = (String[]) htBenefit.get(String.valueOf(a)); 
				String strBenefitName = arrBenefit[0];
				String strBenefitLimit = BBApp_Common.fnFormatNumberInd(arrBenefit[1]);
				
				Element eBenefit = doc.createElement("Benefit");
				eRoot.appendChild(eBenefit);
				
					Element eBenefitName = doc.createElement("BenefitName");
					eBenefit.appendChild(eBenefitName);
					Text txtBenefitName = doc.createTextNode(strBenefitName);
					eBenefitName.appendChild(txtBenefitName);
					
					Element eBenefitLimit = doc.createElement("BenefitLimit");
					eBenefit.appendChild(eBenefitLimit);
					Text txtBenefitLimit = doc.createTextNode(strBenefitLimit);
					eBenefitLimit.appendChild(txtBenefitLimit);
					
					Element ePlanName = doc.createElement("PlanName");
					eBenefit.appendChild(ePlanName);
					Text txtPlanName = doc.createTextNode(strPlanName);
					ePlanName.appendChild(txtPlanName);
					
			}
			
		}
		
		if (req.getSession().getAttribute("lHstDataClaim") != null) {
			
			BBApp_Common.fnWriteLog("BBApp_cMainScreen","BBApp_cMainScreen(XMLOutput) : ","createXmlTree, data klaim");
			
			Hashtable htDataKlaim = (Hashtable) req.getSession().getAttribute("lHstDataClaim");
			
			String strMaxNo = (String) htDataKlaim.get("MaxNo");
			String strRecNo = (String) htDataKlaim.get("RecNo");
			
			Hashtable htResult = (Hashtable) htDataKlaim.get("Register");
			BBApp_cjbREG_MAIN lObjRegMain = (BBApp_cjbREG_MAIN) htResult.get("1");
			
			String strPartClaimRun = BBApp_Common.fnGetValue(lObjRegMain.getPROGRAM_NAME());
			String strKlaimNo = BBApp_Common.fnGetValue(lObjRegMain.getCLAIM_NO());
			String strRegisterDate = BBApp_Common.fnGetValue(lObjRegMain.getREG_DATE());
			String strPolicyNo = BBApp_Common.fnGetValue(lObjRegMain.getPOL_NO());
			String strGroupProgName = BBApp_Common.fnGetValue(lObjRegMain.getRESERVE_TYPE());
			String strDateOfLoss = BBApp_Common.fnGetValue(lObjRegMain.getADMIT_DATE());
			String strLocation = BBApp_Common.fnGetValue(lObjRegMain.getHOSP_CODE());
			String strDiagnosa = BBApp_Common.fnGetValue(lObjRegMain.getDIAGNOSA());
			String strAcceptedNo = BBApp_Common.fnGetValue(lObjRegMain.getACCEPT_NO());
			String strAcceptedDate = BBApp_Common.fnGetValue(lObjRegMain.getAPPROVE_DATE());
			String strTotAccepted = BBApp_Common.fnGetValue(lObjRegMain.getTOTAL_APPROVE());
			String strTotProposed = BBApp_Common.fnGetValue(lObjRegMain.getTOTAL_AMT());
			String strTglLunas = BBApp_Common.fnGetValue(lObjRegMain.getDISCHRG_DATE());
			String strStatusKlaim = BBApp_Common.fnGetValue(lObjRegMain.getCLM_STAT());
			
			String strTglBayar = BBApp_Common.fnGetValue(lObjRegMain.getTGL_BAYAR());
			String strStsProvider = BBApp_Common.fnGetValue(lObjRegMain.getSTS_PROVIDER());
			String strTotalEkses = BBApp_Common.fnGetValue(lObjRegMain.getTOT_EXCESS());
			String strTglLunasEkses = BBApp_Common.fnGetValue(lObjRegMain.getTGL_LUNAS_EKSES());
			
			Element eKlaim = doc.createElement("Klaim");
			eRoot.appendChild(eKlaim);
			
				Element ePartClaimRun = doc.createElement("PartClaimRun");
				eKlaim.appendChild(ePartClaimRun);
				Text txtPartClaimRun = doc.createTextNode(strPartClaimRun);
				ePartClaimRun.appendChild(txtPartClaimRun);
				
				Element eKlaimNo = doc.createElement("KlaimNo");
				eKlaim.appendChild(eKlaimNo);
				Text txtKlaimNo = doc.createTextNode(strKlaimNo);
				eKlaimNo.appendChild(txtKlaimNo);
				
				Element eRegisterDate = doc.createElement("RegisterDate");
				eKlaim.appendChild(eRegisterDate);
				Text txtRegisterDate = doc.createTextNode(strRegisterDate);
				eRegisterDate.appendChild(txtRegisterDate);
				
				Element ePolicyNo = doc.createElement("PolicyNo");
				eKlaim.appendChild(ePolicyNo);
				Text txtPolicyNo = doc.createTextNode(strPolicyNo);
				ePolicyNo.appendChild(txtPolicyNo);
				
				Element eGroupProgName = doc.createElement("GroupProgName");
				eKlaim.appendChild(eGroupProgName);
				Text txtGroupProgName = doc.createTextNode(strGroupProgName);
				eGroupProgName.appendChild(txtGroupProgName);
				
				Element eDateOfLoss = doc.createElement("DateOfLoss");
				eKlaim.appendChild(eDateOfLoss);
				Text txtDateOfLoss = doc.createTextNode(strDateOfLoss);
				eDateOfLoss.appendChild(txtDateOfLoss);
				
				Element eLocation = doc.createElement("Location");
				eKlaim.appendChild(eLocation);
				Text txtLocation = doc.createTextNode(strLocation);
				eLocation.appendChild(txtLocation);
				
				Element eStsProvider = doc.createElement("StsProvider");
				eKlaim.appendChild(eStsProvider);
				Text txtStsProvider = doc.createTextNode(strStsProvider);
				eStsProvider.appendChild(txtStsProvider);
				
				Element eDiagnosa = doc.createElement("Diagnosa");
				eKlaim.appendChild(eDiagnosa);
				Text txtDiagnosa = doc.createTextNode(strDiagnosa);
				eDiagnosa.appendChild(txtDiagnosa);
				
				Element eAcceptedNo = doc.createElement("AcceptedNo");
				eKlaim.appendChild(eAcceptedNo);
				Text txtAcceptedNo = doc.createTextNode(strAcceptedNo);
				eAcceptedNo.appendChild(txtAcceptedNo);

				Element eAcceptedDate = doc.createElement("AcceptedDate");
				eKlaim.appendChild(eAcceptedDate);
				Text txtAcceptedDate = doc.createTextNode(strAcceptedDate);
				eAcceptedDate.appendChild(txtAcceptedDate);
				
				Element eTotProposed = doc.createElement("TotProposed");
				eKlaim.appendChild(eTotProposed);
				Text txtTotProposed = doc.createTextNode(strTotProposed);
				eTotProposed.appendChild(txtTotProposed);
				
				Element eTotAccepted = doc.createElement("TotAccepted");
				eKlaim.appendChild(eTotAccepted);
				Text txtTotAccepted = doc.createTextNode(strTotAccepted);
				eTotAccepted.appendChild(txtTotAccepted);
				
				Element eTglBayar = doc.createElement("TglBayar");
				eKlaim.appendChild(eTglBayar);
				Text txtTglBayar = doc.createTextNode(strTglBayar);
				eTglBayar.appendChild(txtTglBayar);

				Element eTglLunas = doc.createElement("TglLunas");
				eKlaim.appendChild(eTglLunas);
				Text txtTglLunas = doc.createTextNode(strTglLunas);
				eTglLunas.appendChild(txtTglLunas);
				
				Element eStatusKlaim = doc.createElement("StatusKlaim");
				eKlaim.appendChild(eStatusKlaim);
				Text txtStatusKlaim = doc.createTextNode(strStatusKlaim);
				eStatusKlaim.appendChild(txtStatusKlaim);
				
				Element eTotalEkses = doc.createElement("TotalEkses");
				eKlaim.appendChild(eTotalEkses);
				Text txtTotalEkses = doc.createTextNode(strTotalEkses);
				eTotalEkses.appendChild(txtTotalEkses);
				
				Element eTglLunasEkses = doc.createElement("TglLunasEkses");
				eKlaim.appendChild(eTglLunasEkses);
				Text txtTglLunasEkses = doc.createTextNode(strTglLunasEkses);
				eTglLunasEkses.appendChild(txtTglLunasEkses);
				
				Element eMaxNo = doc.createElement("MaxNo");
				eKlaim.appendChild(eMaxNo);
				Text txtMaxNo = doc.createTextNode(strMaxNo);
				eMaxNo.appendChild(txtMaxNo);
				
				Element eRecNo = doc.createElement("RecNo");
				eKlaim.appendChild(eRecNo);
				Text txtRecNo = doc.createTextNode(strRecNo);
				eRecNo.appendChild(txtRecNo);
			
			
		}
			
		//TransformerFactory instance is used to create Transformer objects. 
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        
        // create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
        String xmlString = sw.toString();

        // bikin file dan tulis isi nya 
      File file = new File("c:/newxml.xml");
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
      bw.write(xmlString);
      bw.flush();
      bw.close();
        
        return xmlString;
    }

	
	/**
	 * fnForwardToJsp (input nama file)
	 */
	public void fnForwardToJsp(HttpServletRequest req, HttpServletResponse res, String strNamaFile)throws ServletException, IOException 
	{	
		getServletConfig().getServletContext().getRequestDispatcher(strNamaFile).forward(req,res);
		return;
	}
}
